package com.kaifamiao.store.system.ex;

import com.kaifamiao.store.system.util.Resp;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.lang.reflect.UndeclaredThrowableException;

@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(ServiceException.class)
    public Resp handleServiceException(ServiceException e){
        if (StringUtils.hasText(e.getMessage())){
            return Resp.error(e.getMessage());
        }
        return Resp.error("服务器发生业务异常");
    }

    @ExceptionHandler(UndeclaredThrowableException.class)
    public Resp handleUndeclaredThrowableException(UndeclaredThrowableException e){
        if (e.getCause() != null){
            return Resp.error(e.getCause().getMessage());
        }
        return Resp.error("服务器发生未知异常");
    }

    @ExceptionHandler(Exception.class)
    public Resp handleException(Exception e){
        if (StringUtils.hasText(e.getMessage())){
            return Resp.error(e.getMessage());
        }
        return Resp.error("未知异常");
    }
}
